package com.visionet.waveletData.web.repository.account;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.visionet.waveletData.web.entity.account.User;

public interface UserDao extends PagingAndSortingRepository<User, Long>, JpaSpecificationExecutor<User> {

    //User findBySecId(Long secId);

    @Query("select u.id from User u where u.user_name=?1 and u.id <> ?2 ")
	Long checkByLoginName(String loginName,Long userId);

//    @Query("select count(u.id) from User u where u.isLock=0 and u.aliasName=?1 and u.orgId = ?2 and u.id <> ?3 ")
//	Long checkByAliasName(String aliasName,Long orgId,Long userId);

//	@Query("select count(u) from User u where u.isLock=0 ")
//	Long findUserCount();
	
//	@Query("select count(u.id) from User u inner join u.roleList r where u.isLock=0 and u.orgId = ?2 and r.name in (?1) ")
//	Long getAllUserNum(List<String> roleIds,Long orgId);
	
//	@Query("select u from User u inner join u.roleList r where u.isLock=0 and u.orgId = ?2 and r.name in (?1) order by u.id ")
//	List<User> findAllUserWithRole(List<String> roleIds,Long orgId);
	
//	@Query("select u.id from User u inner join u.roleList r where u.isLock=0 and u.orgId = ?2 and r.name = ?1 order by u.id ")
//	List<Long> findAllUserIdWithRole(String roleId,Long orgId);
	
//	@Query("select u.id,u.loginName,u.aliasName,u.userInfo.phoneNumber,u.orgName,u.userInfo.trueName " +
//            "from User u inner join u.roleList r where u.isLock=0 and u.orgId = ?2 and r.name in (?1) order by u.id ")
//	List<Object[]> findUsersWithRole(List<String> roleIds,Long orgId);
	
//	@Query("select u.id,u.userInfo.trueName,u.userInfo.trueFirstLetter " +
//			"from User u inner join u.roleList r " +
//			" where u.isLock=0 and u.orgId = ?2 and r.name in (?1) " )
//	List<Object[]> searchAllTrueNameUser(List<String> roleIds,Long orgId);
	
	@Query(value = "from User u where u.id in (?1) ")
	List<User> findUsersByIds(List<Long> userIds);
	
	
//	@Modifying
//	@Query("update User u set u.isLock=1 where u.orgId=?1")
//	void deleteUserByOrgId(Long orgId);
	
//	@Deprecated
//	@Modifying
//	@Query("update User u set u.secId = null,u.isLockSec = 0  where u.id=?1 or u.secId=?1")
//	void release(Long leaderId);
	
	@Modifying
//	@Query("update User u set u.isLockSec = ?2  where u.id=?1 and u.isLockSec != ?2")
//	void lockSec(Long leaderId,Long isLockSec);
	
	@Query(" from User u where u.id=?1 ")
	User findUserById(Long id);

//    @Query("select u.id,u.userInfo.trueName,u.userInfo.trueFirstLetter,u.aliasName,u.orgName " +
//            "from User u inner join u.roleList r " +
//            " where u.isLock=0 and u.orgId = ?2 and r.name in (?1) " +
//            " and (u.userInfo.trueFirstLetter = ?3 or u.userInfo.trueName = ?3 ) ")
//    List<Object[]> searchTrueNameUser(List<String> roleIds,Long orgId,String trueName);
    
//    @Query("select u from User u inner join u.roleList r where u.orgId=?1 and r.name in (?2)")
//    List<User> findCompanyAdmin(Long orgId, List<String> roleIds);
    /**
     * 
    * @Title: findByLoginName
    * @Description: 按照登录名查询账号
    * @param @param loginName
    * @param @return    
    * @return User    
    * @throws
     */
    @Query("from User u where u.user_name=?1 and u.is_deleted='0' ")
	User findByLoginName(String loginName);

//    @Query("from User u where u.isLock=0 and u.loginName like %?1%")
//	Page<User> findNoLock(String loginName, Pageable pageable);
//    @Modifying
//    @Query("update User u set u.isLock=1 where u.id=?1")
//	void setUserLock(Long id);
}
